package com.example.coinhelper.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.coinhelper.Bean.User;
import com.example.coinhelper.Manage.UserManager;
import com.example.coinhelper.R;
import com.google.android.material.snackbar.Snackbar;

public class RegisterActivity extends AppCompatActivity {

    private Button ButtonRegister;
    private TextView textUsername;
    private TextView textPassword;
    private TextView textPassword2;
    @SuppressLint({"WrongViewCast", "MissingInflatedId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        textPassword = findViewById(R.id.editTextPassword);
        textPassword2= findViewById(R.id.editTextPassword2);
        textUsername = findViewById(R.id.editTextUsername);
        ButtonRegister = findViewById(R.id.buttonRegister);
        ButtonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password1 = textPassword.getText().toString();
                String password2 = textPassword2.getText().toString();
                String username = textUsername.getText().toString();
                if("".equals(username)||"".equals(password1)||username==null||password1==null){
                    Toast.makeText(RegisterActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(password1.equals(password2)){
                    User user = new User (textUsername.getText().toString(),password2);
                    UserManager userManager = new UserManager(RegisterActivity.this);
                    long status = userManager.addUser(user);
                    if(status<0){
                        Toast.makeText(RegisterActivity.this, "该用户已存在", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                        finish();
                    }
                }else{
                    Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}